﻿ using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DAL.DataAccessObject
{
    public class GiamGiaHHDAO:DataAccess
    {
        public void GiamGiaHH_AddNew(GiamGiaHH gghh)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO GiamGiaHang VALUES(@MaGG,@MaHH,@TGBD,@TGKT,@GhiChu,@ChietKhau)", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaGG", SqlDbType.Char)).Value = gghh.maGiamGia;
            cmd.Parameters.Add(new SqlParameter("@TGBD", SqlDbType.Date)).Value = gghh.thoiGianBatDau.Date;
            cmd.Parameters.Add(new SqlParameter("@TGKT", SqlDbType.Date)).Value = gghh.thoiGianKetThuc.Date;
            cmd.Parameters.Add(new SqlParameter("@GhiChu", SqlDbType.NVarChar)).Value = gghh.ghiChu;
            cmd.Parameters.Add(new SqlParameter("@MaHH", SqlDbType.VarChar)).Value = gghh.maHang;
            cmd.Parameters.Add(new SqlParameter("@ChietKhau", SqlDbType.Int)).Value = gghh.chietKhau;
            cmd.ExecuteNonQuery();
        }
        public void GiamGiaHH_Update(GiamGiaHH gghh)
        {
            SqlCommand cmd = new SqlCommand("UPDATE GiamGiaHang SET NgayBatDau=@TGBD,NgayKetThuc=@TGKT,GhiChu=@GhiChu,ChietKhau=@ChietKhau,MaHang=@MaHH WHERE MaGiamGiaHang=@MaGG", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaGG", SqlDbType.Char)).Value = gghh.maGiamGia;
            cmd.Parameters.Add(new SqlParameter("@TGBD", SqlDbType.Date)).Value = gghh.thoiGianBatDau.Date;
            cmd.Parameters.Add(new SqlParameter("@TGKT", SqlDbType.Date)).Value = gghh.thoiGianKetThuc.Date;
            cmd.Parameters.Add(new SqlParameter("@GhiChu", SqlDbType.NVarChar)).Value = gghh.ghiChu;
            cmd.Parameters.Add(new SqlParameter("@MaHH", SqlDbType.VarChar)).Value = gghh.maHang;
            cmd.Parameters.Add(new SqlParameter("@ChietKhau", SqlDbType.Int)).Value = gghh.chietKhau;
            cmd.ExecuteNonQuery();
        }
        public void GiamGiaHH_Delete(GiamGiaHH gghh)
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM GiamGiaHang WHERE MaGiamGiaHang=@MaGG", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaGG", SqlDbType.Char)).Value = gghh.maGiamGia;
            cmd.ExecuteNonQuery();
        }
        public List<GiamGiaHH> GiamGiaHH_GetData()
        {
            List<GiamGiaHH> listgghh = new List<GiamGiaHH>();
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM GiamGiaHang", GetConnection());
            DataTable dt = new DataTable();
            sda.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                GiamGiaHH gghh = new GiamGiaHH();
                gghh.maGiamGia = dt.Rows[i]["MaGiamGiaHang"].ToString();
                gghh.maHang = dt.Rows[i]["MaHang"].ToString();
                gghh.thoiGianBatDau = Convert.ToDateTime(dt.Rows[i]["NgayBatDau"]);
                gghh.thoiGianKetThuc = Convert.ToDateTime(dt.Rows[i]["NgayKetThuc"]);
                gghh.ghiChu = dt.Rows[i]["GhiChu"].ToString();
                gghh.chietKhau = Convert.ToInt32(dt.Rows[i]["ChietKhau"]);
                listgghh.Add(gghh);
            }
            return listgghh;
        }
        public List<GiamGiaHH> GiamGiaHH_SerachByGoods(string a)
        {
            List<GiamGiaHH> listgghh = new List<GiamGiaHH>();
            SqlCommand cmd = new SqlCommand("SELECT * FROM GiamGiaHang", GetConnection());
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (a == dr["MaHang"].ToString().Trim())
                {
                    GiamGiaHH gghh = new GiamGiaHH();
                    gghh.maGiamGia = dr["MaGiamGiaHang"].ToString();
                    gghh.thoiGianBatDau = Convert.ToDateTime(dr["NgayBatDau"]);
                    gghh.thoiGianKetThuc = Convert.ToDateTime(dr["NgayKetThuc"]);
                    gghh.maHang = dr["MaHang"].ToString();
                    gghh.chietKhau = Convert.ToInt32(dr["ChietKhau"]);
                    gghh.ghiChu = dr["GhiChu"].ToString();
                    listgghh.Add(gghh);
                }
            }
            return listgghh;
        }
        public DataView SelectAllViewGGH()
        {
            SqlCommand cmd = new SqlCommand("select * from GiamGiaHang", GetConnection());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return new DataView(dt);
        }
    }
}
